package com.part2.action.system;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.part2.beans.News;
import com.part2.business.function.NewsService;

public class IndexServletContextListener implements ServletContextListener  {

	private ServletContext context = null;
	private NewsService service;

	
	/* 这个方法在Web应用服务被移除，没有能力再接受请求的时候被调用。
	 */
	public void contextDestroyed(ServletContextEvent event){
		//Output a simple message to the server's console
	}

	// 这个方法在Web应用服务做好接受请求的时候被调用。
	public void contextInitialized(ServletContextEvent event){
		this.context = event.getServletContext();
		 ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
		 this.service = (NewsService)ctx.getBean("newsService");
		List<News> list =service.findNews();
		context.setAttribute("nList", list);
		//Output a simple message to the server's console
//		context.setAttribute(arg0, arg1)
	}
}
